﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

namespace WcfService.DBLayer
{
    public class DBConnection
    {
        public static readonly string _connectionString = @"Data Source=balder.ucn.dk;database=dm75_4;User id=DM75_4; Password=MaaGodt";
        public static readonly SqlConnection _dbConn = new SqlConnection(_connectionString);
        private static SqlCommand _dbCmd;

        private static void Open()
        { 
            if (_dbConn.State != System.Data.ConnectionState.Open)
                _dbConn.Open();
            
        }

        public static void Close()
        {
            _dbConn.Close();
        }

        public static SqlCommand GetDbCommand(string sql)
        {
            if (_dbConn.State != System.Data.ConnectionState.Open)
            {
                _dbConn.Open();
            }
            if (_dbCmd == null)
            {
                _dbCmd = new SqlCommand(sql, _dbConn);
            }
            _dbCmd.Parameters.Clear();  
            _dbCmd.CommandText = sql;

            return _dbCmd;
        }
    }
}